#include "stdafx.h"
#include <iostream>
#include "ImportDataConfig.h"
#include "../Tinyxml2/tinyxml2.h"

ImportDataConfig::ImportDataConfig()
{
}


ImportDataConfig::ImportDataConfig(const std::string &config_file)
    : config_file_(config_file)
{
    loadFile(config_file_);
}


ImportDataConfig::~ImportDataConfig()
{
}

void ImportDataConfig::loadFile(const std::string &config_file)
{
    tinyxml2::XMLDocument doc;
    auto ret = doc.LoadFile(config_file.c_str());
    if (ret != tinyxml2::XML_SUCCESS)
    {
        std::cout << "load config file faild: " << config_file << ", error code is " << ret << std::endl;
        return;
    }

    auto import_data = doc.FirstChildElement("importData");

    if (import_data == nullptr)
    {
        return;
    }

    if (auto database = import_data->FirstChildElement("database"))
    {
        database_.load(database);
    }

    if (auto tables = import_data->FirstChildElement("tables"))
    {
        auto table_element = tables->FirstChildElement("table");

        while (table_element)
        {
            TableConfig table;
            table.load(table_element);

            tables_.push_back(table);

            table_element = table_element->NextSiblingElement("table");
        }
    }
}
